package com.sojson.project.controller;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.project.controller.param.verify.OrderDepositParamVerify;
import com.sojson.project.entity.bo.StringParam;
import com.sojson.project.entity.po.OrderDeposit;
import com.sojson.project.entity.vo.OrderDepositVo;
import com.sojson.project.service.OrderDepositService;
import com.sojson.project.sys.user.entity.bo.AuthcUser;
import com.sojson.result.Result;
import com.sojson.util.ExpUtil;
import com.sojson.util.StringUtil;
import com.sojson.util.token.TokenUtil;

import io.swagger.annotations.Api;

/**
 * 诚意金订单表控制器
 * 
 * @author liu
 * @date 2021-04-29
 */
@Api(tags = "诚意金订单表管理")
@RestController
@RequestMapping("/orderDeposit")
public class OrderDepositController {

    @Autowired
    private OrderDepositService orderDepositService;

    /**
     * 单条插入
     * 
     * @param orderDeposit
     * @return
     */
    // @PostMapping("/insert")
    public Object insert(@RequestBody OrderDeposit orderDeposit) {
        return Result.success(orderDepositService.insert(orderDeposit));
    }

    /**
     * 批量插入
     * 
     * @param orderDeposit
     * @return
     */
    // @PostMapping("/insertList")
    public Object insertList(@RequestBody OrderDeposit orderDeposit) {
        return Result.success(orderDepositService.insertList(orderDeposit));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param orderDeposit
     * @return
     */
    // @DeleteMapping("/deleteByDto")
    public Object deleteByDto(@RequestBody OrderDeposit orderDeposit) {
        return Result.success(orderDepositService.deleteByDto(orderDeposit));
    }

    /**
     * 统计全部数量
     * 
     * @return
     */
    // @GetMapping("/countAll")
    public Object countAll() {
        return Result.success(orderDepositService.countAll());
    }

    /**
     * 查询所有
     * 
     * @return
     */
    // @GetMapping("/findAll")
    public Object findAll() {
        return Result.success(orderDepositService.findAll());
    }

    /**
     * 根据对象统计数量
     * 
     * @param orderDeposit
     * @return
     */
    // @GetMapping("/countByDto")
    public Object countByDto(OrderDeposit orderDeposit) {
        return Result.success(orderDepositService.countByDto(orderDeposit));
    }

    /**
     * 根据对象查询
     * 
     * @param orderDeposit
     * @return
     */
    @GetMapping("/findByDto")
    @PreAuthorize("@ss.hasPermi('/orderDeposit/findByDto')")
    public Result<List<OrderDepositVo>> findByDto(OrderDeposit orderDeposit) {
        if (StringUtil.isBlankString(orderDeposit.getOrderSn())) {
            orderDeposit.setOrderSn(null);
        }
        List<OrderDepositVo> findByDto = orderDepositService.list(orderDeposit);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     */
    // @DeleteMapping("/deleteById")
    public Object deleteById(Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(orderDepositService.deleteById(id));
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     */
    // @DeleteMapping("/deleteByIdList")
    public Object deleteByIdList(@RequestParam("ids") List<Integer> ids) {
        if (ids.size() == 0) {
            return Result.success();
        }

        return Result.success(orderDepositService.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param orderDeposit
     * @return
     */
    // @PutMapping("/updateById")
    public Object updateById(@RequestBody OrderDeposit orderDeposit) {
        return Result.success(orderDepositService.updateById(orderDeposit));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param orderDeposit
     * @return
     */
    // @PutMapping("/updateStaticByIdList")
    public Object updateStaticByIdList(@RequestBody OrderDeposit orderDeposit) {
        return Result.success(orderDepositService.updateStaticByIdList(orderDeposit));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param orderDeposit
     * @return
     */
    // @PutMapping("/updateChangeByIdList")
    public Object updateChangeByIdList(@RequestBody OrderDeposit orderDeposit) {
        return Result.success(orderDepositService.updateChangeByIdList(orderDeposit));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param orderDeposit
     * @return
     */
    // @PutMapping("/updatePartialById")
    public Object updatePartialById(@RequestBody OrderDeposit orderDeposit) {
        return Result.success(orderDepositService.updatePartialById(orderDeposit));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param orderDeposit
     * @return
     */
    // @PutMapping("/updateStaticPartialByIdList")
    public Object updateStaticPartialByIdList(@RequestBody OrderDeposit orderDeposit) {
        return Result.success(orderDepositService.updateStaticPartialByIdList(orderDeposit));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param orderDeposit
     * @return
     */
    // @PutMapping("/updateChangePartialByIdList")
    public Object updateChangePartialByIdList(@RequestBody OrderDeposit orderDeposit) {
        return Result.success(orderDepositService.updateChangePartialByIdList(orderDeposit));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/findById")
    @PreAuthorize("@ss.login()")
    public Object findById(String id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(orderDepositService.findById(id));
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     */
    // @GetMapping("/findByIdList")
    public Object findByIdList(@RequestParam("ids[]") List<Integer> ids) {
        if (ids.size() == 0) {
            return Result.success();
        }

        return Result.success(orderDepositService.findByIdList(ids));
    }

    /**
     * 通过对象查询id列表
     * 
     * @param orderDeposit
     * @return
     */
    // @GetMapping("/findIdByDto")
    public Object findIdByDto(OrderDeposit orderDeposit) {
        return Result.success(orderDepositService.findIdByDto(orderDeposit));
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     */
    // @DeleteMapping("/clear")
    public Object clear() {
        orderDepositService.clear();
        return Result.success();
    }

    /**
     * 下单
     * 
     * @return
     * @throws Exception 
     */
    @PostMapping(value = "/orders")
    @PreAuthorize("@ss.login()")
    public Object orders(@RequestBody AuthcUser authcUser) throws Exception {
        String payPwd = authcUser.getPayPwd();
        return Result.success(orderDepositService.orders(payPwd));
    }

    /**
     * 取消订单
     * 
     * @param id 订单ID
     * @return
     * @throws Exception 
     */
    // @PutMapping(value = "/cancel")
    public Object cancel(@RequestBody StringParam id) throws Exception {
        String val = id.getVal();
        // 校验参数
        Result<?> paramResult = OrderDepositParamVerify.cancel(val);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        return Result.success(orderDepositService.cancel(val));
    }

    /**
     * 退诚意金
     * 
     * @return
     * @throws Exception 
     */
    @PutMapping(value = "/refund")
    @PreAuthorize("@ss.login()")
    public Object refund(@RequestBody AuthcUser authcUser) throws Exception {
        return Result.success(orderDepositService.refund());
    }

    /**
     * 退诚意金
     * 
     * @return
     * @throws Exception 
     */
    @PutMapping(value = "/refundByOrderSn")
    @PreAuthorize("@ss.login()")
    public Object refundByOrderSn(@RequestBody OrderDepositVo orderDeposit) throws Exception {
        if (StringUtil.isBlankObject(orderDeposit.getOrderSn())) {
            return Result.fail("诚意金订单ID不能为空！");
        }
        return Result.success(orderDepositService.refund(orderDeposit));
    }

    /**
     * 根据对象查询
     * 
     * @param orderDeposit
     * @return
     * @throws IOException 
     */
    @GetMapping("/findAppByDto")
    @PreAuthorize("@ss.login()")
    public Object findAppByDto(OrderDeposit orderDeposit) throws IOException {
        orderDeposit.setMid(TokenUtil.getUserId());
        return Result.success(orderDepositService.findByDto(orderDeposit));
    }

    /**
     * 将用户诚意金转入余额
     * 
     * @param id
     * @return
     * @throws Exception 
     */
    @PutMapping("/toBrokerage")
    @PreAuthorize("@ss.hasPermi('/orderDeposit/toBrokerage')")
    public Object toBrokerage(@RequestBody OrderDeposit orderDeposit) throws Exception {
        if (StringUtil.isBlankObject(orderDeposit.getId())) {
            ExpUtil.throwEx("诚意金订单ID不能为空！");
        }
        return Result.success(orderDepositService.toBrokerage(orderDeposit));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param orderDeposit
     * @return
     */
    @PutMapping("/updateStatus")
    @PreAuthorize("@ss.hasPermi('/orderDeposit/updateStatus')")
    public Object updateStatus(@RequestBody OrderDepositVo orderDeposit) {
        String id = orderDeposit.getId();
        if (StringUtil.isBlankObject(id)) {
            return Result.fail("诚意金订单ID不能为空！");
        }
        Integer status = orderDeposit.getStatus();
        if (StringUtil.isBlankObject(status)) {
            return Result.fail("诚意金订单状态不能为空！");
        }

        OrderDeposit od = new OrderDeposit();
        od.updateInit();
        od.setId(id);
        od.setStatus(status);
        return Result.success(orderDepositService.updateStatus(orderDeposit));
    }

}